package com.info33.platform.business.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.business.app.dao.AppMenuFastDao;
import com.info33.platform.business.app.dto.AppMenuFastDTO;
import com.info33.platform.business.app.entity.AppMenuFast;
import com.info33.platform.business.app.service.AppMenuFastService;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.vo.SysUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author admin
 */
@Service
public class AppMenuFastServiceImpl extends ServiceImpl<AppMenuFastDao, AppMenuFast> implements AppMenuFastService {

    @Autowired
    private AppMenuFastDao appMenuFastDao;

    @Override
    public Result<Object> appChange(AppMenuFastDTO dto, SysUserVO appCurrentUser) {
        if (dto.getMenuIds().split(TableConstant.COMMA).length > 9) {
            return Result.error("菜单个数上限：9，超出上限无法设置");
        }
        AppMenuFast entity = new AppMenuFast();
        BeanUtils.copyProperties(dto, entity);
        AppMenuFast old = appMenuFastDao.selectOne(new QueryWrapper<AppMenuFast>()
                .select(TableConstant.ID)
                .eq(TableConstant.CREATE_BY, appCurrentUser.getAppTenantUserVO().getId())
                .last(TableConstant.LIMIT_ONE));
        if (!ObjectUtils.isEmpty(old)) {
            entity.setId(old.getId());
        }
        try {
            return this.saveOrUpdate(entity) ? Result.ok() : Result.error();
        } catch (Exception e) {
            return Result.error(Result.RESULT_ERROR_PUBLIC_MSG);
        }
    }
}